package com.admin.controller.admin.email;

import com.system.response.mail.MailRecordDtoResponse;
import com.system.service.mail.MailRecordService;
import com.system.util.MailUtil;
import com.system.annotation.Log;
import com.system.constant.HTTPStatus;
import com.system.emus.BusinessType;
import com.system.response.Result;
import com.system.response.mail.MailDtoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname SendMailController
 * @Date 2023/8/9 9:34
 * @Created ZFC
 */
@RestController
@RequestMapping("/sys/mail")
public class SendMailController {

    @Autowired
    private MailUtil mailUtil;

    @Autowired
    private MailRecordService mailRecordService;

    @PostMapping("/sendMail")
    @Log(title = "发送邮件",businessType = BusinessType.OTHER)
    public Result sendMail(@RequestBody MailDtoResponse mailDto){
        mailUtil.sendMailMessageHtml(mailDto.getTo(),mailDto.getSubject(),mailDto.getText());
        return new Result().setCode(HTTPStatus.SUCCESS).setMessage("邮件发送成功");
    }
    
    @Cacheable(
            value = "mail_record",
            key = "#mailRecordDto.pageSize +'::'+ #mailRecordDto.pageNum +'::'+ " +
                    "#mailRecordDto.subject +'::'+ #mailRecordDto.recipientsMail +'::'+ " +
                    "#mailRecordDto.status +'::'+ #mailRecordDto.startTime +'::'+ " +
                    "#mailRecordDto.endTime"
    )
    @GetMapping("/mailList")
    public Result mailList(MailRecordDtoResponse mailRecordDto){
        return mailRecordService.mailList(mailRecordDto);
    }

    @DeleteMapping("/deleteMail")
    public Result deleteMail(Integer[] ids){
        return mailRecordService.deleteMailRecord(ids);
    }

    @CacheEvict(
            value = "mail_record",
            allEntries = true
    )
    @DeleteMapping("/clearMailRecordCache")
    public Result clearMailRecordCache(){
        return new Result().setCode(HTTPStatus.SUCCESS).setMessage("清除成功！");
    }

    @DeleteMapping("/clearMailRecord")
    public Result clearMailRecord(){
        return mailRecordService.clearMailRecord();
    }
}
